import './css/login.css'
import { Button, Checkbox, Form, Input, message } from 'antd';
import logo from '../assets/images/logo192.png'
import { login } from '../api/user';
import { NavLink, useNavigate } from 'react-router-dom'


export default function Login() {

    const navigate = useNavigate()

    const onFinish = (values) => {
        login(values).then(res => {
            console.log(res)
            switch (res.Code) {
                case 1:
                    message.success('登录成功！！')
                    navigate('/home', {
                        replace: false,
                        state: {
                            name: res.name,
                            headerImg: res.headerImg
                        }
                    })
                    break
                case 0:
                    message.warning('用户名或密码错误！！！')
                    break
                case -1:
                    message.error('程序内部错误！！！')
                    break
                default:
            }
        }).catch(err => console.log('错误' + err))
    };

    const onFinishFailed = (errorInfo) => {
        console.log('Failed:', errorInfo);
    };

    return (
        <div id="login">
            <Form
                name="basic"
                labelCol={{ span: 8 }}
                wrapperCol={{ span: 16 }}
                initialValues={{ remember: true }}
                onFinish={onFinish}
                onFinishFailed={onFinishFailed}
                autoComplete="off"
            >
                <h2><img src={logo} alt='xxx'/>登录界面</h2>
                <Form.Item
                    label="用户名"
                    name="username"
                    rules={[{ required: true, message: '请输入用户名！' }]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="密码"
                    name="password"
                    rules={[{ required: true, message: '请输入密码！' }]}
                >
                    <Input.Password />
                </Form.Item>

                <Form.Item name="remember" valuePropName="checked" wrapperCol={{ offset: 8, span: 16 }}>
                    <Checkbox>记住用户名密码</Checkbox>
                </Form.Item>

                <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
                    <Button type="primary" htmlType="submit">
                        登录
                    </Button>
                    <NavLink to='/register'>
                        <Button type="primary">
                            注册
                        </Button>
                    </NavLink>
                    <NavLink to='/home'>
                        <Button type="primary">
                            返回
                        </Button>
                    </NavLink>
                </Form.Item>
            </Form>
        </div>
    )
}